package com.dingmouren.smaple.paletteimageview.adapter;

import com.dingmouren.paletteimageview.PaletteImageView;
import com.dingmouren.paletteimageview.factory.PixelMapFactory;
import com.dingmouren.smaple.paletteimageview.ResourceTable;
import com.dingmouren.smaple.paletteimageview.util.ComponentUtil;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.PageSliderProvider;
import ohos.app.Context;
import ohos.media.image.PixelMap;

import java.util.Arrays;

public class GalleryProvider extends PageSliderProvider {

    private final Context context;

    private final int[] pictures;

    public GalleryProvider(Context context, int[] pictures) {
        this.context = context;
        if (pictures != null) {
            this.pictures = Arrays.copyOf(pictures, pictures.length);
        } else {
            this.pictures = null;
        }
    }

    @Override
    public int getCount() {
        return pictures.length;
    }

    @Override
    public Object createPageInContainer(ComponentContainer componentContainer, int i) {

        Component component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_item_layout_of_gallery, null, false);
        PaletteImageView image = ComponentUtil.findComponent(component, ResourceTable.Id_image);
        PixelMap pixelMap = PixelMapFactory.CreatePixelMap(context, pictures[i]);
        image.setPixelMap(pixelMap);
        componentContainer.addComponent(component);
        return component;
    }

    @Override
    public void destroyPageFromContainer(ComponentContainer componentContainer, int i, Object o) {
        componentContainer.removeComponent((Component) o);
    }

    @Override
    public boolean isPageMatchToObject(Component component, Object o) {
        return true;
    }


}
